/**
 * @Description:support.js
 * @Author:LCore
 */

// var documentWidth = window.screen.availWidth
// var gridContainerWidth = 0.92 * documentWidth
// var cellSideLength = 0.18 * documentWidth
// var cellSpace = 0.04 * documentWidth
// var headerWidth = window.screen.availWidth
var cellSideLength
var cellSpace

var showText = {
  '2': '高雄萌妹',
  '4': '一发入魂',
  '8': '乱斗王者',
  '16': 'CAD大师',
  '32': '斗奶狂魔',
  '64': '认发作妈',
  '128': '下岗女工',
  '256': '梦碎豪门',
  '512': '太极宗师',
  '1024': '远离魔都',
  '2048': '文学巨匠',
  '4096': '分手大师',
  '8192': '金牌僚机',
  '16384': '股海明灯',
}
var backgroundColor = {
  '2': '#eee4da',
  '4': '#ede0c8',
  '8': '#f2b179',
  '16': '#f59563',
  '32': '#f67c5f',
  '64': '#f65e3b',
  '128': '#edcf72',
  '256': '#edcc61',
  '512': '#9c0',
  '1024': '#33b5e5',
  '2048': '#09c',
  '4096': '#a6c',
  '8192': '#93c',
  '16384': '#e04',
}

export default class Support {
  constructor(w, h) {
    this.w = w
    this.h = h
    cellSideLength = 0.18*w
    cellSpace = 0.04*w
  }

  
  getPostionTop(i, j) {
    return (i * cellSideLength + (i + 1) * cellSpace + 0.3 * this.w + 50);
  }

  getPostionLeft(i, j) {
    return (j * cellSideLength + (j + 1) * cellSpace+ 0.04 * this.w);
  }

  getShowTextByNum(number) {
    if (number < (16384 + 1)) {
      return showText[number];
    }

    return 'fafa';
  }

  getBackgroundColorByNum(number) {
    if (number < (16384 + 1)) {
      return backgroundColor[number];
    }
    return "black";
  }

  getPreColorByNum(number) {
    if (number <= 4)
      return "#776e65";
    return "white";
  }

  isNoSpace(board) {
    for (var i = 0; i < 4; i++)
      for (var j = 0; j < 4; j++)
        if (board[i][j] == 0)
          return false;

    return true;
  }

  canMoveLeft(board) {
    for (var i = 0; i < 4; i++) {
      for (var j = 1; j < 4; j++) {
        if (board[i][j] != 0) {
          if (board[i][j - 1] == 0 || board[i][j] == board[i][j - 1])
            return true;
        }
      }
    }
    return false;
  }

  canMoveRight(board) {
    for (var i = 3; i >= 0; i--) {
      for (var j = 2; j >= 0; j--) {
        if (board[i][j] != 0) {
          if (board[i][j + 1] == 0 || board[i][j] == board[i][j + 1])
            return true;
        }

      }
    }
    return false;
  }

  /**
   *@Desciption: 补全原方法, 添加全部方向都不能的判断
   *@author: huwhois
   *@pragram board:棋盘格
   *@pragram direction:移动方向
   */
  canMoveUp(board) {
    for (var i = 0; i < 4; i++) {
      for (var j = 1; j < 4; j++) {
        if (board[j][i] != 0) { //可以移动
          if (board[j - 1][i] == 0 || board[j][i] == board[j - 1][i])
            return true;
        }
      }
    }
    return false;
  }

  canMoveDown(board) {
    for (var i = 3; i >= 0; i--) {
      for (var j = 2; j >= 0; j--) {
        if (board[j][i] != 0) { //可以移动
          if (board[j + 1][i] == 0 || board[j][i] == board[j + 1][i])
            return true;
        }
      }
    }
    return false;
  }

  canMoveAll(board) {
    return (this.canMoveLeft(board) || this.canMoveRight(board) || this.canMoveUp(board) || this.canMoveDown(board))
  }

  /**
   *@Desciption:重新构造canMove方法(包括上、下、左、右)
   *@author:LCore
   *@pragram board:棋盘格
   *@pragram direction:移动方向
   */

  canMove(board, direction) {
    if (direction == 1) {
      //左移
      for (var i = 0; i < 4; i++) {
        for (var j = 1; j < 4; j++) {
          if (board[i][j] != 0) {
            if (board[i][j - 1] == 0 || board[i][j] == board[i][j - 1])
              return true;
          }

        }
      }
    } else if (direction == 2) {
      //上移
      for (var i = 0; i < 4; i++) {
        for (var j = 1; j < 4; j++) {
          if (board[j][i] != 0) { //可以移动
            if (board[j - 1][i] == 0 || board[j][i] == board[j - 1][i])
              return true;
          }
        }
      }
    } else if (direction == 3) {
      //下移
      for (var i = 3; i >= 0; i--) {
        for (var j = 2; j >= 0; j--) {
          if (board[j][i] != 0) { //可以移动
            if (board[j + 1][i] == 0 || board[j][i] == board[j + 1][i])
              return true;
          }
        }
      }
    } else if (direction == 4) {
      //右移
      for (var i = 3; i >= 0; i--) {
        for (var j = 2; j >= 0; j--) {
          if (board[i][j] != 0) {
            if (board[i][j + 1] == 0 || board[i][j] == board[i][j + 1])
              return true;
          }

        }
      }
    } else
      return false;

  }

  /**
   *@Description:横向判断是否存在障碍物
   *@pagram row:第几行
   *@pagram col1:开始索引
   *@pagram col2:结束索引
   *@pagram board:棋盘格
   */
  noBlockHorizontal(row, col1, col2, board) {
    for (var i = col1 + 1; i < col2; i++) {
      if (board[row][i] != 0)
        return false;
    }
    return true;
  }

  /**
   *@Description:纵向判断是否存在障碍物
   *@pagram col:第几列
   *@pagram row1:开始索引
   *@pagram row2:结束索引
   *@pagram board:棋盘格
   */
  noBlockVectal(col, row1, row2, board) {
    for (var i = row1 + 1; i < row2; i++) {
      if (board[i][col] != 0)
        return false;
    }
    return true;
  }
}